//
//  XJNet.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJNet.h"

static XJNet *request = nil;


@implementation XJNet

+ (instancetype)loadNet{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        request = [[XJNet alloc]init];
    });
    return request;
    
}

- (void)netWorkReachabilityState:(void(^)(BOOL))state{
    //1.创建网络状态监测管理者
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                state(NO);
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                state(YES);
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                state(YES);
                break;
            default:
                break;
        }
    }];
    [manger startMonitoring];
}

- (void)requestWithType:(XJNetType)type
            requestPath:(NSString *)path
             dictionary:(NSDictionary *)dic
       returnValueBlock:(XJRetureValueBlock)returnBlock
           failureBlock:(XJFailureBlock)failureBlock
         errorCodeBlock:(XJErrorCodeBlock)errorBlock{
    
    if (type==XJNetTypePost) {
        AFSecurityPolicy *securityPolicy=[[AFSecurityPolicy alloc]init];
        [securityPolicy setAllowInvalidCertificates:YES];
        AFHTTPSessionManager *sessionManager=[AFHTTPSessionManager  manager];
        [sessionManager setSecurityPolicy:securityPolicy];
        sessionManager.responseSerializer=[AFHTTPResponseSerializer serializer];
        sessionManager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];
        [sessionManager POST:path parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //设置超时时间
            [sessionManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            sessionManager.requestSerializer.timeoutInterval = 5.0f;
            [sessionManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            
            NSError *errors;
            NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
            NSString *retStr = [[NSString alloc] initWithData:responseObject encoding:enc];
            NSData *data = [retStr   dataUsingEncoding:NSUTF8StringEncoding];
            // XXLog(@"POST_data :%@",data);
            NSDictionary *dictionaryData;
            
            if (data==NULL) {
                XJLog(@"POST_UTF8解析！");
                NSString *datata=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
                XJLog(@"POST_data :%@",datata);
                dictionaryData = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errors];
            }else{
                XJLog(@"POST_enc解析！");
                dictionaryData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errors];
            }
            returnBlock(dictionaryData);
            XJLog(@"POST_returnBlock_data %@",dictionaryData);
            if (errors) {
                errorBlock(errors);
                XJLog(@"POST_errorBlock_error %@",errors);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failureBlock();
            XJLog(@"POST_failureBlock_error%@", error);
        }];
    }else if (type==XJNetTypeGet){
        AFHTTPSessionManager *Session = [AFHTTPSessionManager manager];
        Session.responseSerializer=[AFHTTPResponseSerializer serializer];
        Session.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];
        //设置超时时间
        [Session.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        Session.requestSerializer.timeoutInterval = 5.0f;
        [Session.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        [Session GET:path parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
             XJLog(@"GET_returnBlock_data %@",data);
            returnBlock(data);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failureBlock();
            XJLog(@"GET_failureBlock_error%@", error);
        }];
    }
}

@end
